﻿using MediatR;
using Microsoft.IdentityModel.Tokens;
using W.Device.Domain;
using W.Device.Domain.DTO;
using W.Device.Read.API.Application.Command;
using W.Device.Repository.Interface;

namespace W.Device.Read.API.Application.CommandHandlder
{
    public class DeviceShowCommandHandler:IRequestHandler<DeviceShowCommand, List<DevicesInfo>>
    {
        private readonly IDeviceRepository deviceRepository;

        public DeviceShowCommandHandler(IDeviceRepository deviceRepository)
        {
            this.deviceRepository = deviceRepository;
        }

        public async Task<List<DevicesInfo>> Handle(DeviceShowCommand request, CancellationToken cancellationToken)
        {
            var list = await deviceRepository.GetAsync(w=>w.IsDelete==false);
            if(request.DeviceName!="")
            {
                list = list.Where(w=>w.DeviceName.Contains(request.DeviceName)).ToList();
            }
            if(request.DeviceNo!="")
            {
                list = list.Where(w=>w.DeviceNo==request.DeviceNo).ToList();
            }
            return list.ToList();
        }
    }
}
